package com.vframework.config.activemq;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.jms.support.converter.MessageConverter;

import com.vframework.jms.EopConsumer;
import com.vframework.jms.EopMessageConverter;

@Configuration
@EnableJms
public class JmsConfiguration {
    
    @Value("${spring.activemq.broker-url}")
    private String URL;
    @Value("${spring.activemq.user}")
    private String USER;
    @Value("${spring.activemq.password}")
    private String PASSWORD;
    
    // 创建ActiveMQ连接工厂
    @Bean
    public ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(URL);
        connectionFactory.setUserName(USER);
        connectionFactory.setPassword(PASSWORD);
        connectionFactory.setTrustAllPackages(true);
        connectionFactory.setRedeliveryPolicy(redeliveryPolicy());
        return connectionFactory;
    }

    /**
     * 消息重试配置项
     * 
     * @return
     */
    @Bean
    public RedeliveryPolicy redeliveryPolicy() {
        RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
        redeliveryPolicy.setUseExponentialBackOff(true);//是否在每次失败重发是，增长等待时间
        redeliveryPolicy.setMaximumRedeliveryDelay(-1);// 设置重发最大拖延时间，-1表示没有拖延，只有setUseExponentialBackOff(true)时生效
        redeliveryPolicy.setMaximumRedeliveries(10);// 重发次数
        redeliveryPolicy.setInitialRedeliveryDelay(1);// 重发时间间隔
        redeliveryPolicy.setBackOffMultiplier(2);// 第一次失败后重发前等待500毫秒，第二次500*2，依次递增
        redeliveryPolicy.setUseCollisionAvoidance(false);// 是否避免消息碰撞
        return redeliveryPolicy;
    }

    // 设置JmsTemplate
    @Bean
    public JmsTemplate jmsQueueTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
        jmsTemplate.setMessageConverter(eopMessageConverter());
        jmsTemplate.setDeliveryMode(2);//设置持久化，1 非持久， 2 持久化
        return jmsTemplate;
    }

    @Bean
    public MessageListenerAdapter messageListener() {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(new EopConsumer());
        messageListenerAdapter.setMessageConverter(eopMessageConverter());
        messageListenerAdapter.setDefaultListenerMethod("execute");
        return messageListenerAdapter;
    }

    // 设置queue监听器
    @Bean
    public DefaultMessageListenerContainer jmsQueueListenerContainerFactory() {
        DefaultMessageListenerContainer factory = new DefaultMessageListenerContainer();
        factory.setConnectionFactory(connectionFactory());
        factory.setMessageConverter(eopMessageConverter());
        factory.setMessageListener(messageListener());
        factory.setDestinationName("riskqueue");
        factory.setConcurrency("3-10");
        factory.setRecoveryInterval(1000L);
        return factory;
    }

    // 设置消息转换器
    @Bean
    public MessageConverter eopMessageConverter() {
        EopMessageConverter eopMessageConverter = new EopMessageConverter();
        return eopMessageConverter;
    }
}